/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class MileageTrackerTestSuite {

	static testMethod void runPositiveTestCases() {
		// TO DO: implement unit test
		Double totalMiles = 0;
		final Double maxtotalMiles= 500;
		final Double singletotalMiles= 300;
		final String createdbyId = UserInfo.getUserId();
		List<Mileage_second__c> deleteMiles = new List<Mileage_second__c>();

		//Data clean-up
		System.debug('Setting up testing - deleting any mileage records for today');
		deleteMiles = [select Miles__c from Mileage_second__c where createdDate = TODAY
				and createdById = :createdbyId];
		if(!deleteMiles.isEmpty()){
			delete deleteMiles;
		}

		//Positive tests
		System.debug('Insterting 300 more miles...sigle record validation');
		Mileage_second__c testMiles1 = new Mileage_second__c(Miles__c = 300, Date__c = System.today() );
		insert testMiles1;

		//Validate single insert
		for(Mileage_second__c m:[SELECT Miles__c FROM Mileage_second__c
				WHERE createdDate = TODAY
				and   createdById = :createdbyId
				and   Miles__c != null]){
			totalMiles += m.miles__c;
		}
		System.assertEquals(maxtotalMiles, totalMiles);
		totalMiles = 0;
		
		//Validate bulk insert
		System.debug('Inserting 200 more miles...bulk validation');
		List<Mileage_second__c> testMiles2 = new List<Mileage_second__c>();
		for(integer i=0; i<200; i++){
			testMiles2.add( new Mileage_second__c(Miles__c = 1, Date__c = System.today()));
		}
		insert testMiles2;
		//Assert Mileage
		for(Mileage_second__c m:[SELECT miles__c FROM Mileage_second__c
				WHERE createdDate = TODAY
				and   createdById = :createdbyId
				and   miles__c != null]){
			totalMiles += m.miles__c;
		}
		System.assertEquals(maxtotalMiles, totalMiles);
	}
	//Negative tests
	static testMethod void runNegativeTestCases(){
		System.debug('Inserting 501 miles... negative test case');
		Mileage_second__c testMiles3 = new Mileage_second__c(Miles__c = 501,Date__c = System.today());
		//Assert Error Message
		try{
			insert testMiles3;
		} catch (Dmlexception e){
			//Assert Error Message
			System.assert(e.getMessage().contains('Insert failed. ' +
			'First exeption on row 0; first error: ' +
			'FIELD CUSTOM VALIDATION EXCEPTION, ' +
			'Mileage request exceeds daily limit(500): [Miles__c]'),
			e.getMessage());
			//Assert Field
			System.assertEquals(Mileage_second__c.Miles__c, e.getDmlFields(0)[0]);
			//Assert Status Code
			System.assertEquals('FIELD CUSTOM VALIDATION EXCEPTION',
				e.getDmlStatusCode(0));
		}
	}
}